import ir6

def gen_result(ans_list,ques_point,ques,ans_flag):
	"""tries to generate the response page
	"""
	#now process the ans_list [word,current_sent_toks,sent_point, total_point, url,clickurl,abstract]
	if ans_list==None:
		return '''Content-Type: text/html\n
		<html>
		<head>
		<title>AQuAS's reply</title>
		</head>
		<body>
		<h1 align="LEFT">AQuAS's reply</h1>
		<p>
		<img height="3" width="100%" src="http://localhost/images/line-rainbow.gif">
		</p>
		<p>
		<i>You asked:</i> <b>'''+ques+'''</b></p>
		<p> I am sorry, that I don't the answer to your question
		</p>
		</body></html>
		'''
	ans=ans_list[0]
	
	word=''
	for i in ans[0]:
		if ques_point=='DATE':
			t2=str(i)
			if t2!='-1':
				word+=t2
				word+=' '
		else:
			t2=str(i)
			word+=t2
			word+=' '
	
	word=word.rstrip()
	
	abstract=ans[6]
	url=ans[4]
	
	if ques_point=='GPE':
		rp0='''Content-Type: text/html\n
		<html>
		<head>
		<title>AQuAS's reply</title>
		<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
		<script type="text/javascript">
		var geocoder;
		var map;
		function initialize(address) 
		{
			geocoder = new google.maps.Geocoder();
			if (geocoder) 
			{
				geocoder.geocode( { 'address': address}, function(results, status) 
				{
					if (status == google.maps.GeocoderStatus.OK) 
					{
						var myOptions = {
						zoom: 8,
						center: results[0].geometry.location,
						mapTypeId: google.maps.MapTypeId.ROADMAP
						}
						map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
						var marker = new google.maps.Marker({
						map: map, 
						position: results[0].geometry.location
						});
					} else 
					{
						alert("Geocode was not successful for the following reason: " + status);
					}
				});
			}
		}
		
		</script>
		</head>
		'''
	else:
		rp0='''Content-Type: text/html\n
		<html>
		<head>
		<title>AQuAS's reply</title>
		</head>
		'''
		
	rp1="""<body onload="initialize('"""+word+"""')">"""
	
	rp2='''<h1 align="LEFT">AQuAS's reply</h1>'''
	
	rp3='''<p>
	<img height="3" width="100%" src="http://localhost/images/line-rainbow.gif">
	</p>
	'''
	
	rp4='''<p>
	<i>You asked:</i> <b>'''+ques+'''</b></p>'''
	
	if ans_flag==2:
		rp5='''<p>
		<i>You can find details here:</i>
		'''+abstract+'''</p>'''
	else:
		rp5='''<p>
		<i>answer: </i><b>'''+word+'''</b></p>'''
	if ans_flag!=2:
		rp6='''<p>
		<i>deduced from: </i>'''+abstract+'''</p>'''
	else:
		rp6=''
	
	if ques_point=='GPE':
		rp7='''\n<div id="map_canvas" style="width:50%; height:50%"></div>'''
	elif ques_point=='PERSON' and ans_flag!=2:
		img_details=ir6.get_image_details(word)
		if img_details!=None:
			h=img_details[1]
			w=img_details[2]
			img_url=img_details[0]
			rem=int(h)/250
			if rem==0:
				rem=1
			h=int(h)/rem
			w=int(w)/rem
			rp7='''<img src="'''+img_url+'''" height="'''+str(h)+'''" width="'''+str(w)+'''" >'''
		else:
			rp7=''
	else:
		rp7=''
	
	rp8='''<p> <a href="'''+url+'''"><i>Source</i></a></p>'''
	
	rp9=rp3
	
	rp10='''</body></html>'''
	
	response=rp0+rp1+rp2+rp3+rp4+rp5+rp6+rp7+rp8+rp9+rp10
	
	return response
